﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class PurchaseApp
    {
        private readonly PurchaseComposeRepository _servicePurchaseCompose = new PurchaseComposeRepository();
        private readonly PurchaseRepository _service = new PurchaseRepository();
        private readonly CustomerRepository _customerservice = new CustomerRepository();
        private readonly PurchasePrintRepository _purchasePrintservice = new PurchasePrintRepository();
        private readonly PurchasePrintSubRepository _purchasePrintSubservice = new PurchasePrintSubRepository();
        private readonly PurchaseReconciliationRepository _prservice = new PurchaseReconciliationRepository();

        public IEnumerable<ProductTechnologyView> GetList(int status, int type, string creatorName, string productName, DateTime beginTime, DateTime endTime)
        {
            return _service.GetList(status, type, creatorName, productName, beginTime, endTime).OrderByDescending(a => a.ApplyTime);
        }

        public IEnumerable<ProductTechnologyView> GetPurList(int status, int type, string creatorName, string productName, DateTime beginTime, DateTime endTime)
        {
            return _service.GetPurList(status, type, creatorName, productName, beginTime, endTime).OrderBy(a => a.PurchaseStatus).ThenByDescending(a => a.ApplyTime);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <param name="productId"></param>
        /// <param name="purchaseType">1：合并采购 2:单独采购</param>
        /// <returns></returns>
        public PurchaseCompose GetSinglePurchaseCompose(int status, Guid productId, int purchaseType)
        {
            return _servicePurchaseCompose.FindEntity(a => a.ProductId == productId && a.Status == status && a.PurchaseType == purchaseType);
        }
        public PurchaseCompose GetSinglePurchaseComposeById(Guid pcId)
        {
            return _servicePurchaseCompose.FindEntityNoTracking(a => a.Id == pcId);
        }

        public PurchaseCompose GetSingleEarlistPurchaseComposeBypId(Guid productId)
        {
            return _servicePurchaseCompose.IQueryable(a => a.ProductId == productId).OrderByDescending(a => a.CreateTime).FirstOrDefault();
        }

        public IEnumerable<PurchaseCompose> GetSubListPurchaseComposeById(Guid pcId)
        {
            return _servicePurchaseCompose.IQueryable(a => a.ParentId == pcId);
        }

        public int UpdatePurchaseCompose(PurchaseCompose web)
        {
            return _servicePurchaseCompose.Update(web);
        }

        public int InsertPurchaseCompose(PurchaseCompose web)
        {
            return _servicePurchaseCompose.Insert(web);
        }

        public Purchase GetSingle(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }

        public IEnumerable<Customer> GetCustomerList(int type)
        {
            return _customerservice.IQueryable(a => a.Type == type).OrderByDescending(a => a.CreateTime);
        }
        public Customer GetSingleCustomer(int type, Guid id)
        {
            return _customerservice.FindEntity(a => a.Type == type && a.Id == id);
        }

        public int Update(Purchase web)
        {
            return _service.Update(web);
        }
        public int Add(Purchase web)
        {
            return _service.Insert(web);
        }

        public int AddPurchaseReconciliation(PurchaseReconciliation web)
        {
            return _prservice.Insert(web);
        }

        public IEnumerable<ReconciliationView> GetListReconciliation(int status, string financeNumber, string productName, string supplierName)
        {
            return _prservice.GetListReconciliation(status, financeNumber, productName, supplierName).OrderBy(a => a.Status).ThenByDescending(a => a.ApplyTime);
        }

        public ReconciliationView GetSingleReconciliation(Guid id)
        {
            return _prservice.GetSingleReconciliation(id);
        }
        public PurchaseReconciliation GetSinglePurchaseReconciliation(Guid id)
        {
            return _prservice.FindEntity(a => a.Id == id);
        }
        public int UpdatePurchaseReconciliation(PurchaseReconciliation web)
        {
            return _prservice.Update(web);
        }

        public int AddPurchasePrint(PurchasePrint web)
        {
            return _purchasePrintservice.Insert(web);
        }

        public int AddPurchasePrintSub(PurchasePrintSub web)
        {
            return _purchasePrintSubservice.Insert(web);
        }

        public int UpdatePurchasePrint(PurchasePrint web)
        {
            return _purchasePrintservice.Update(web);
        }

        public int UpdatePurchasePrintSub(PurchasePrintSub web)
        {
            return _purchasePrintSubservice.Update(web);
        }

        public PurchasePrint GetSinglePurchasePrint(Guid supplierId, DateTime benginTime, DateTime endTime)
        {
            return _purchasePrintservice.FindEntity(a => a.SupplierId == supplierId
                && a.Status == 0
                && a.CreateTime >= benginTime
                && a.CreateTime < endTime);
        }

        public PurchasePrintSub GetSinglePurchasePrintSub(Guid supplierId, Guid productId, decimal costPrice, DateTime benginTime, DateTime endTime)
        {
            return _purchasePrintSubservice.FindEntity(a => a.SupplierId == supplierId
                && a.ProductId == productId
                && a.Status == 0
                && a.CostPrice == costPrice
                && a.CreateTime >= benginTime
                && a.CreateTime < endTime);
        }

        public PurchasePrintSub GetSinglePurchasePrintSubByother(Guid productId, Guid otherId)
        {
            return _purchasePrintSubservice.FindEntity(a => a.OtherId == otherId && a.ProductId == productId);
        }

        public PurchasePrint GetSinglePurchasePrintBack(Guid id)
        {
            return _purchasePrintservice.FindEntity(a => a.Id == id);
        }

        public IEnumerable<OrderView> GetListPurchasePrint(string supplierName)
        {
            return _purchasePrintservice.GetListPurchasePrint(supplierName).OrderBy(a => a.Status).ThenByDescending(a => a.CreateTime);
        }

        public IEnumerable<NewProductView> GetListPurchasePrintSub(Guid id)
        {
            return _purchasePrintSubservice.GetListPurchasePrintSub(id);
        }
    }
}
